<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of c_cart
 *
 * @author Tuyet
 */
class yourcart extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->model("m_sanpham");
        $this->load->library('cart');
    }

    public function index() {
        $this->load->view('demo_cart');
    }
    
    public function addtocart() {
        $id_product = trim($_GET['id']);
        $qty = trim($_GET['qty']);
        $size = trim($_GET['size']);
        $product = new M_sanpham();
        $product = $product->getProduct($id_product);
        $image = $product['product_thumb_image'];
        $price = $product['price_now'];
        $name_product = $product['product_name'];
        if ($this->check($id, $qty)) {
            $data = array(
                'id' => $id_product,
                'qty' => $qty,
                'price' => $price,
                'name' => $name_product,
                'options' => array('Size' => $size, 'image' => $image)
            );

            $this->cart->insert($data);
        }
    }

    function check($id, $qty) {
        $data = $this->cart->contents();
        foreach ($this->cart->contents() as $item) {
            if ($item['id'] == $id) {
                $num = intval($item['qty']) + intval($qty);
                echo $num;
                $cartitem = array('rowid' => $item['rowid'], 'qty' => $num);
                $this->cart->update($cartitem);
                return false;
            }
        }
        return true;
    }

    public function updatetocart() {
        
    }

    public function deletetocart() {
        
    }

}

?>
